<?php

namespace app\user\controller;

use app\bbs\model\BbsUserFans;
use app\common\service\UserService;
use app\user\validate\UserValidate;
use app\user\model\User;
use app\common\attributes\RandyValidate;
use app\common\attributes\Menu;
use think\annotation\Inject;
use think\response\Json;
use think\App;

class UserController extends UserBaseController
{

    protected User $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new User();
    }

    #[Inject]
    protected UserService $service;

    #[menu('查看顾客组账号列表')]
    public function index() : Json
    {
        $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $list = $this->service->getUserList($extendQueryHelper, $data);
        foreach ($list as $row) {
        }
        return $this->success($list);
    }

    #[menu('查看顾客组账号列表')]
    public function list()
    {
        $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $extendQueryHelper->setIsPage(false);
        $list = $this->service->getUserList($extendQueryHelper, $data);
        foreach ($list as $row) {
        }
        return $this->success($list);
    }


    #[menu('查看顾客组账号详情')]
    public function read($user_id) : Json
    {
        $res = $this->service->getUserItem($user_id);
        if (is_client()) {
            $fans = BbsUserFans::build()
                               ->where('user_id', $user_id)
                               ->where('fans_id', get_client_id())
                               ->find();
            $being_follow = BbsUserFans::build()
                                       ->where('user_id', get_client_id())
                                       ->where('fans_id', $user_id)
                                       ->find();
            $res->is_follow = $fans ? 1 : 0;
            $res->is_being_follow = $being_follow ? 1 : 0;
        }
        return $this->returnSuccess($res);
    }

    #[menu('删除顾客组账号')]
    public function del($user_id) : Json
    {
        $res = $this->service->delUserItem($user_id);
        return $this->returnSuccess($res);
    }

    #[menu('更新顾客组账号')]
    #[RandyValidate(validate: UserValidate::class, scene: 'update')]
    public function update($user_id) : \think\response\Json
    {
        $data = $this->getParams();
        $res = $this->service->setUserItem($user_id, $data);
        return $this->returnSuccess();
    }

    #[menu('新增顾客组账号')]
    #[RandyValidate(validate: UserValidate::class, scene: 'save')]
    public function save() : Json
    {
        $data = $this->getParams();
        $res = $this->service->addUserItem($data);
        return $this->returnSuccess($res);
    }

    #[menu('禁用顾客组账号')]
    public function disable($user_id) : Json
    {
        $res = $this->service->disableUserItem($user_id);
        return $this->returnSuccess($res);
    }

    #[menu('批量禁用顾客组账号')]
    public function batchDisable($user_ids) : Json
    {
        $res = $this->service->batchDisable($user_ids);
        return $this->returnSuccess($res);
    }

    #[menu('批量禁用顾客组账号')]
    public function batchUnDisable($user_ids) : Json
    {
        $res = $this->service->batchUnDisable($user_ids);
        return $this->returnSuccess($res);
    }

    #[menu('禁用顾客组账号')]
    public function unDisable($user_id) : Json
    {
        $res = $this->service->unDisableUserItem($user_id);
        return $this->returnSuccess($res);
    }

    #[Menu('修改用户昵称')]
    public function updateNickname($nickname, $user_id) : \think\response\Json
    {
        $res = $this->service->updateNickname($nickname, $user_id);
        return $this->returnSuccess($res);
    }


    public function updateHeadPic($head_pic, $user_id) : \think\response\Json
    {
        $res = $this->service->updateHeadPic($head_pic, $user_id);
        return $this->returnSuccess($res);
    }

    #[Menu('获取用户详情汇总数据')]
    public function summary($user_id)
    {
        $res = $this->service->summary($user_id);
        return $this->returnSuccess($res);
    }

}
